package com.joeljhou.project.form;

import com.joeljhou.project.core.SerialPortManager;
import com.joeljhou.project.core.listener.AboutListener;
import com.joeljhou.project.core.listener.ClearListener;
import com.joeljhou.project.core.listener.CloseListener;
import com.joeljhou.project.core.listener.ExitListener;
import com.joeljhou.project.core.listener.OpenListener;
import com.joeljhou.project.core.listener.SaveListener;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class SerialPortForm {

    public static void initialization() {
        Component component = Component.getInstance();
        component.setJFrame(new MyJFram());
        JFrame jf = component.getJFrame();
        //窗口大小是否可调整
        jf.setMinimumSize(new Dimension(900, 450));
        jf.setResizable(true);

        //顶部菜单栏组件
        JPanel jMenuBar = getJMenuBar();
        jf.add(jMenuBar, BorderLayout.NORTH);

        //核心面板(边界布局:将容器分成五个区域：北/NORTH、南/SOUTH、东/EAST、西/WEST和中心/CENTER)
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        //图标按钮
        JPanel iconButtons = getIconButtons();
        mainPanel.add(iconButtons, BorderLayout.NORTH);

        //串口设置窗口
        JPanel configPanel = getConfigPanel();
        mainPanel.add(configPanel, BorderLayout.WEST);

        //数据展示窗口
        JPanel dataPanel = getDataPanel();
        mainPanel.add(dataPanel, BorderLayout.CENTER);

        //操作日志
        JPanel logPanel = getLogPanel();
        mainPanel.add(logPanel, BorderLayout.SOUTH);

        jf.add(mainPanel, BorderLayout.CENTER);
        jf.setVisible(true);
    }

    //顶部菜单栏组件
    private static JPanel getJMenuBar() {
        Component component = Component.getInstance();

        JPanel menuBarPanel = new JPanel();
        menuBarPanel.setLayout(new BoxLayout(menuBarPanel, BoxLayout.X_AXIS));  //指定组件从左到右布局

        Dimension dimension = new Dimension(60, 24);

        JMenuBar bar = new JMenuBar();
        JMenu menu1 = new JMenu("文件(F)");
        menu1.setPreferredSize(dimension);
        menu1.setMinimumSize(dimension);

        component.setSaveMenu(new JMenuItem("保存(S)"));
        JMenuItem item11 = component.getSaveMenu();
        item11.setIcon(getImageIcon("/image/保存x18.png"));
        item11.addActionListener(new SaveListener());

        component.setClearMenu(new JMenuItem("清除(C)"));
        JMenuItem item12 = component.getClearMenu();
        item12.setIcon(getImageIcon("/image/清除x18.png"));
        item12.addActionListener(new ClearListener());

        component.setExitMenu(new JMenuItem("退出(X)"));
        JMenuItem item13 = component.getExitMenu();
        item13.setIcon(getImageIcon("/image/退出x18.png"));
        item13.addActionListener(new ExitListener());

        menu1.add(item11);
        menu1.add(item12);
        menu1.add(item13);

        JMenu menu2 = new JMenu("编辑(E)");
        menu2.setPreferredSize(dimension);
        menu2.setMinimumSize(dimension);
        component.setOpenMenu(new JMenuItem("开始(F)"));
        JMenuItem item21 = component.getOpenMenu();
        item21.setIcon(getImageIcon("/image/开始x18.png"));
        item21.addActionListener(new OpenListener());

        component.setCloseMenu(new JMenuItem("停止(T)"));
        JMenuItem item22 = component.getCloseMenu();
        item22.setIcon(getImageIcon("/image/停止x18.png"));
        item22.addActionListener(new CloseListener());

        menu2.add(item21);
        menu2.add(item22);

        JMenu menu3 = new JMenu("帮助(H)");
        menu3.setPreferredSize(dimension);
        menu3.setMinimumSize(dimension);
        JMenuItem item31 = new JMenuItem("关于(A)");
        item31.setIcon(getImageIcon("/image/关于x18.png"));
        item31.addActionListener(new AboutListener());

        menu3.add(item31);

        bar.add(menu1);
        bar.add(menu2);
        bar.add(menu3);

        menuBarPanel.add(bar);
        return menuBarPanel;
    }

    //顶部图标按钮
    private static JPanel getIconButtons() {
        Component component = Component.getInstance();

        JPanel iconButtons = new JPanel();
        iconButtons.setLayout(new BoxLayout(iconButtons, BoxLayout.X_AXIS));  //指定组件从左到右布局
        iconButtons.setPreferredSize(new Dimension(900, 36));
        iconButtons.setBorder(BorderFactory.createEtchedBorder());            //蚀刻边框

        //保存
        ImageIcon icon1 = getImageIcon("/image/保存x32.png");
        component.setSaveIconButton(getIconJButton(icon1));
        JButton button1 = component.getSaveIconButton();
        button1.addActionListener(new SaveListener());

        //清除
        ImageIcon icon2 = getImageIcon("/image/清除x32.png");
        component.setClearIconButton(getIconJButton(icon2));
        JButton button2 = component.getClearIconButton();
        button2.addActionListener(new ClearListener());

        //开始
        ImageIcon icon3 = getImageIcon("/image/开始x32.png");
        component.setOpenIconButton(getIconJButton(icon3));
        JButton button3 = component.getOpenIconButton();
        button3.addActionListener(new OpenListener());

        //停止
        ImageIcon icon4 = getImageIcon("/image/停止x32.png");
        component.setCloseIconButton(getIconJButton(icon4));
        JButton button4 = component.getCloseIconButton();
        button4.addActionListener(new CloseListener());

        iconButtons.add(button1);
        iconButtons.add(button2);
        iconButtons.add(button3);
        iconButtons.add(button4);
        return iconButtons;
    }

    //左侧串口设置窗口
    private static JPanel getConfigPanel() {
        Component component = Component.getInstance();
        //设置展示面板
        JPanel configShowPanel = new JPanel(new BorderLayout());
        configShowPanel.setPreferredSize(new Dimension(315, 0));
        configShowPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "串口设置"));

        JPanel configPanel = new JPanel();
        configPanel.setLayout(null);
        configPanel.setPreferredSize(new Dimension(0, 300));

        JLabel portLabel = new JLabel("端        口:");
        portLabel.setBounds(20, 20, 60, 28);
        configPanel.add(portLabel);

        component.setPortComboBox(new JComboBox());
        JComboBox portComboBox = component.getPortComboBox();
        portComboBox.addItem(" 请选择指定串口 ");
        //获取本地可用的串口信息
        List<String> availableSerialPorts = SerialPortManager.getAvailableSerialPorts();
        for (String port : availableSerialPorts) {
            portComboBox.addItem(port);
        }
        portComboBox.setBounds(85, 20, 205, 28);
        configPanel.add(portComboBox);

        JLabel pullLabel = new JLabel("拉  力  值:");
        pullLabel.setBounds(20, 58, 60, 28);
        configPanel.add(pullLabel);

        component.setPullTextField(new JTextField());
        JTextField pullTextField = component.getPullTextField();
        pullTextField.setBounds(85, 58, 205, 28);
        configPanel.add(pullTextField);

        JLabel buckleModelLabel = new JLabel("扣子型号:");
        buckleModelLabel.setBounds(20, 96, 60, 28);
        configPanel.add(buckleModelLabel);

        component.setBuckleModelTextField(new JTextField());
        JTextField buckleModelTextField = component.getBuckleModelTextField();
        buckleModelTextField.setBounds(85, 96, 205, 28);
        configPanel.add(buckleModelTextField);

        JLabel buYuanLabel = new JLabel("布原(mm):");
        buYuanLabel.setBounds(20, 134, 60, 28);
        configPanel.add(buYuanLabel);

        component.setBuYuanTextField(new JTextField());
        JTextField buYuanTextField = component.getBuYuanTextField();
        buYuanTextField.setBounds(85, 134, 205, 28);
        configPanel.add(buYuanTextField);

        JLabel machineTypeLabel = new JLabel("机        型:");
        machineTypeLabel.setBounds(20, 172, 60, 28);
        configPanel.add(machineTypeLabel);

        component.setMachineTypeTextField(new JTextField());
        JTextField machineTypeTextField = component.getMachineTypeTextField();
        machineTypeTextField.setBounds(85, 172, 205, 28);
        configPanel.add(machineTypeTextField);

        JLabel auditorLabel = new JLabel("审  核  人:");
        auditorLabel.setBounds(20, 210, 60, 28);
        configPanel.add(auditorLabel);

        component.setAuditorTextField(new JTextField());
        JTextField auditorTextField = component.getAuditorTextField();
        auditorTextField.setBounds(85, 210, 205, 28);
        configPanel.add(auditorTextField);

        JLabel testerLabel = new JLabel("检  测  人:");
        testerLabel.setBounds(20, 248, 60, 28);
        configPanel.add(testerLabel);

        component.setTesterTextField(new JTextField());
        JTextField testerTextField = component.getTesterTextField();
        testerTextField.setBounds(85, 248, 205, 28);
        configPanel.add(testerTextField);

        configShowPanel.add(configPanel, BorderLayout.NORTH);

        // 创建一个表格模型，并设置列名和数据
        DefaultTableModel model = new DefaultTableModel(new String[]{"属性", "值"}, 0);
        model.addRow(new Object[]{"最大值", ""});
        model.addRow(new Object[]{"最小值", ""});
        model.addRow(new Object[]{"平均值", ""});
        model.addRow(new Object[]{"总数量", ""});
        model.addRow(new Object[]{"良品数", ""});
        model.addRow(new Object[]{"非良品数", ""});
        component.setStatisticsTable(new JTable(model));
        JTable table = component.getStatisticsTable();
        table.setRowHeight(26);                                    //单元格高度
        //单元格内容居中
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
            @Override
            public java.awt.Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                //单元格内容居中
                this.setHorizontalAlignment(JLabel.CENTER);
                java.awt.Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if (row == 5 && column == 1 && !value.equals("0")) {  // 判断单元格是否为第六行第二列
                    cell.setForeground(Color.RED);    // 设置单元格字体颜色为红色
                } else {
                    cell.setForeground(Color.BLACK);  // 设置单元格字体颜色为黑色
                }
                return cell;
            }
        };
        table.setDefaultRenderer(Object.class, renderer);
        table.setDefaultEditor(Object.class, null);          //设置单元格不可编辑

        JScrollPane scrollPane = new JScrollPane(table);           //创建table滚动面板
        scrollPane.setColumnHeaderView(table.getTableHeader());    //设置表头
        configShowPanel.add(scrollPane, BorderLayout.CENTER);

        return configShowPanel;
    }

    //右边数据展示面板
    private static JPanel getDataPanel() {
        Component component = Component.getInstance();
        JPanel dataPanel = new JPanel(new BorderLayout());
        dataPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "数据展示"));

        //串口接收面板
        JPanel portPanel = new JPanel(new BorderLayout());  //边界布局
        portPanel.setBorder(BorderFactory.createLoweredBevelBorder());  //斜面边框(凹)
        dataPanel.add(portPanel, BorderLayout.CENTER);

        component.setDataArea(new JTextPane());
        JTextPane area = component.getDataArea();
        area.setEnabled(true);
        area.setFocusable(false);

        JScrollPane scrollPane = new JScrollPane(area);
        portPanel.add(scrollPane, BorderLayout.CENTER);

        //OK/NG
        JPanel calePanel = new JPanel();
        calePanel.setLayout(new BoxLayout(calePanel, BoxLayout.X_AXIS));
        calePanel.setPreferredSize(new Dimension(0, 32));

        // 创建开关按钮
        JLabel bthLeft = new JLabel("NG");
        EmptyBorder border = new EmptyBorder(0, 5, 0, 5);  //设置JLabel的左右空白距离为 10 像素
        bthLeft.setBorder(border);
        component.setToggleBtn(getSwitchButton());
        JToggleButton toggleBtn = component.getToggleBtn();
        JLabel bthRight = new JLabel("ON");
        bthRight.setBorder(border);

        calePanel.add(bthLeft);
        calePanel.add(toggleBtn);
        calePanel.add(bthRight);

        dataPanel.add(calePanel, BorderLayout.SOUTH);
        return dataPanel;
    }


    //底部日志展示
    private static JPanel getLogPanel() {
        Component component = Component.getInstance();
        JPanel logPanel = new JPanel();
        logPanel.setLayout(new BoxLayout(logPanel, BoxLayout.X_AXIS));  //指定组件从左到右布局
        logPanel.setPreferredSize(new Dimension(900, 32));
        logPanel.setBorder(BorderFactory.createEtchedBorder());            //蚀刻边框

        JLabel iconLabel = new JLabel();
        ImageIcon icon = getImageIcon("/image/触控(红)x32.png");
        iconLabel.setIcon(icon);

        component.setOperaLabel(new JLabel());
        JLabel operaLabel = component.getOperaLabel();
        operaLabel.setText("操作日志！");

        logPanel.add(iconLabel);
        logPanel.add(operaLabel);
        return logPanel;
    }

    //获取图标
    private static ImageIcon getImageIcon(String path) {
        return new ImageIcon(SerialPortForm.class.getResource(path));
    }

    //获取图标按钮
    private static JButton getIconJButton(ImageIcon icon) {
        JButton button = new JButton(icon);
        Dimension dimension1 = new Dimension(36, 36);
        button.setPreferredSize(dimension1);
        button.setMinimumSize(dimension1);
        button.setMaximumSize(dimension1);
        button.setOpaque(false);             //true:按钮的背景将被绘制    /false:按钮的背景将不会被绘制
        button.setContentAreaFilled(false);  //true:按钮的内容区域将被绘制 /false:按钮的内容区域将不会被绘制
        button.setBorderPainted(true);       //true:按钮的边框将被绘制    /false:按钮的边框将不会被绘制
        button.setFocusPainted(true);       //true:获得焦点时将绘制焦点框 /false:获得焦点时不会绘制焦点框
        return button;
    }

    //按钮开关
    private static JToggleButton getSwitchButton() {
        JToggleButton toggleBtn = new JToggleButton();
        toggleBtn.setSelectedIcon(new ImageIcon(SerialPortForm.class.getResource("/image/toggle_on.png")));
        toggleBtn.setIcon(new ImageIcon(SerialPortForm.class.getResource("/image/toggle_off.png")));
        Dimension dimension = new Dimension(32, 32);
        toggleBtn.setPreferredSize(dimension);
        toggleBtn.setMinimumSize(dimension);
        toggleBtn.setMaximumSize(dimension);
        toggleBtn.setBorder(null);              //不显示边框
        toggleBtn.setOpaque(false);             //true:按钮的背景将被绘制    /false:按钮的背景将不会被绘制
        toggleBtn.setContentAreaFilled(false);  //true:按钮的内容区域将被绘制 /false:按钮的内容区域将不会被绘制
        toggleBtn.setBorderPainted(true);       //true:按钮的边框将被绘制    /false:按钮的边框将不会被绘制
        toggleBtn.setFocusPainted(true);        //true:获得焦点时将绘制焦点框 /false:获得焦点时不会绘制焦点框
        return toggleBtn;
    }
}
